package com.dongdong.tbbrandService.impl;

import com.dongdong.mapper.TbSpecificationMapper;
import com.dongdong.mapper.TbSpecificationOptionMapper;
import com.dongdong.pojo.*;
import com.dongdong.service.TbSpecificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;


@Service
@com.alibaba.dubbo.config.annotation.Service
public class TbSpecificationServiceImpl implements TbSpecificationService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Transactional(readOnly=true)
    @Override
    public PageBean<TbSpecification> getShow(String specName, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSpecification>list= tbSpecificationMapper.getShow(specName);
        PageInfo<TbSpecification>pageInfo = new PageInfo<>(list);

            return new PageBean<TbSpecification>(pageInfo.getTotal(),pageInfo.getPages(),pageInfo.getStartRow(),pageInfo.getPageSize(),pageInfo.getPageNum(),pageInfo.getList());
    }
    @Transactional(isolation= Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public Boolean save(List<TbSpecificationOption> list, TbSpecification tbSpecification) {
         int i = tbSpecificationMapper.saveTbSpecification(tbSpecification);
        Long id = tbSpecification.getId();
       tbSpecificationOptionMapper.insertSpecification(id,list);
        return i>0;
    }



    @Override
    public List<TbSpecifType> update(Long id) {
        List<TbSpecifType>list = tbSpecificationOptionMapper.selectInfo(id);
        return list;
    }
    @Transactional(isolation= Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public Boolean deleteAll(Long[] ids) {
       int i =  tbSpecificationMapper.deleteAll(ids);
        tbSpecificationOptionMapper.deleteMatch(ids);
        return i>0;
    }
    @Transactional(isolation= Isolation.DEFAULT,propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void updateInfo(TbSpecification tbSpecification, List<TbSpecificationOption> list) {
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        TbSpecificationOptionExample toe = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = toe.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(toe);
        tbSpecificationOptionMapper.insertSpecification(tbSpecification.getId(),list);
    }

    @Override
    public List<TbSpecification> getShowTo() {
        return tbSpecificationMapper.selectByExample(null);
    }

    @Override
    public List<TbSpecificationOption> getShowId() {
        return tbSpecificationOptionMapper.selectByExample(null);
    }
}
